package com.tangji.signaltower.task;

import com.tangji.signaltower.bean.MarketDataEntity;
import com.tangji.signaltower.container.DataContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/7/22
 * Time: 5:22 下午
 */
public class PushDataContainer {


    private static final Logger logger = LoggerFactory.getLogger(PushDataContainer.class);

    private String productType;

    private MarketDataEntity lastData = null;

    private int size = 0;
    private boolean isHandle = false;

    private List<MarketDataEntity> last5DataList;
    Map<Integer, MarketDataEntity> latestData = new HashMap<>();

    public PushDataContainer(String productType, MarketDataEntity lastData) {
        this.productType = productType;
        this.lastData = lastData;
    }




    public MarketDataEntity getFixData(MarketDataEntity marketDataEntity) {

        size++;
        latestData.put(size, marketDataEntity);
        // TODO: 2021/7/22 考虑增加长度 减少替换次数
        if (size >= 5) {
            if (!isHandle) {
                isHandle = true;
            }
            size = 0;
        }

        try {
            if (isHandle) {
                logger.info("== setLastData 原始价格为 ==,price:{}", marketDataEntity.getMarketPrice());
                Map<Integer, MarketDataEntity> latestData2 = new HashMap<>();
                latestData2.put(1, latestData.get(2));
                latestData2.put(2, latestData.get(3));
                latestData2.put(3, latestData.get(4));
                latestData2.put(4, latestData.get(5));
//                logger.info("setLastData 初始数据,data:{},marketDataEntity:{}", JSON.toJSONString(latestData), JsonStream.serialize(marketDataEntity));
                float[] controlPoint = getControlPoint(latestData.get(3), latestData.get(4), latestData.get(5), marketDataEntity);
//                marketDataEntity.setMarketPrice(Float.toString(controlPoint[3]));
                latestData2.put(5, marketDataEntity);
                latestData = latestData2;
                logger.info("== setLastData 调整后格为 ==,price:{}", controlPoint[3]);
            }

            this.lastData = marketDataEntity;
        } catch (Exception e) {
            logger.error("== setLastData ==", e);
            this.lastData = marketDataEntity;
        }

        return this.lastData;
    }


    private static float[] getControlPoint(MarketDataEntity p0, MarketDataEntity p1, MarketDataEntity p2, MarketDataEntity p3) {
        long x0 = p0.getBusinessTime();
        long x1 = p1.getBusinessTime();
        long x2 = p2.getBusinessTime();
        long x3 = p3.getBusinessTime();
        float y0 = Float.parseFloat(p0.getMarketPrice());
        float y1 = Float.parseFloat(p0.getMarketPrice());
        float y2 = Float.parseFloat(p0.getMarketPrice());
        float y3 = Float.parseFloat(p0.getMarketPrice());

        double xc1 = (x0 + x1) / 2.0;
        double yc1 = (y0 + y1) / 2.0;
        double xc2 = (x1 + x2) / 2.0;
        double yc2 = (y1 + y2) / 2.0;
        double xc3 = (x2 + x3) / 2.0;
        double yc3 = (y2 + y3) / 2.0;

        double len1 = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
        double len2 = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
        double len3 = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));

        double k1 = len1 / (len1 + len2);
        double k2 = len2 / (len2 + len3);

        double xm1 = xc1 + (xc2 - xc1) * k1;
        double ym1 = yc1 + (yc2 - yc1) * k1;

        double xm2 = xc2 + (xc3 - xc2) * k2;
        double ym2 = yc2 + (yc3 - yc2) * k2;

        // Resulting control points. Here smooth_value is mentioned
        // above coefficient K whose value should be in range [0...1].
        double k = .1;

        float ctrl1_x = (float) (xm1 + (xc2 - xm1) * k + x1 - xm1);
        float ctrl1_y = (float) (ym1 + (yc2 - ym1) * k + y1 - ym1);

        float ctrl2_x = (float) (xm2 + (xc2 - xm2) * k + x2 - xm2);
        float ctrl2_y = (float) (ym2 + (yc2 - ym2) * k + y2 - ym2);


        return new float[]{ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y};
    }

}
